{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Shadertoys\n",
    "\n",
    "本笔记本演示了如何显示 [shadertoys](https://www.shadertoy.com/)。查看文档以了解更多信息:  \n",
    "https://jupylet.readthedocs.io/en/latest/programmers_reference_guide/graphics-3d.html#shadertoys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sys.path.insert(0, os.path.abspath('./..'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from jupylet.app import App\n",
    "from jupylet.label import Label\n",
    "from jupylet.shadertoy import Shadertoy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from jupylet.audio.bundle import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "app = App(width=533, height=300)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Star Nest Shadertoy by Pablo Roman Andrioli\n",
    "\n",
    "下面单元格中的代码是[beautiful shadertoy by Pablo Roman Andrioli](https://www.shadertoy.com/view/XlfGRj)的代码。它是根据 MIT License授权的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "st = Shadertoy(\"\"\"\n",
    "\n",
    "    // Star Nest by Pablo Roman Andrioli\n",
    "\n",
    "    // This content is under the MIT License.\n",
    "    // 内容基于 MIT License 授权\n",
    "\n",
    "    #define iterations 17\n",
    "    #define formuparam 0.53\n",
    "\n",
    "    #define volsteps 20\n",
    "    #define stepsize 0.1\n",
    "\n",
    "    #define zoom   0.800\n",
    "    #define tile   0.850\n",
    "    #define speed  0.010 \n",
    "\n",
    "    #define brightness 0.0015\n",
    "    #define darkmatter 0.300\n",
    "    #define distfading 0.730\n",
    "    #define saturation 0.850\n",
    "\n",
    "\n",
    "    void mainImage( out vec4 fragColor, in vec2 fragCoord )\n",
    "    {\n",
    "        //get coords and direction\n",
    "        //获得协调和方向\n",
    "        vec2 uv=fragCoord.xy/iResolution.xy-.5;\n",
    "        uv.y*=iResolution.y/iResolution.x;\n",
    "        vec3 dir=vec3(uv*zoom,1.);\n",
    "        float time=iTime*speed+.25;\n",
    "\n",
    "        //mouse rotation\n",
    "        //鼠标旋转\n",
    "        float a1=.5+iMouse.x/iResolution.x*2.;\n",
    "        float a2=.8+iMouse.y/iResolution.y*2.;\n",
    "        mat2 rot1=mat2(cos(a1),sin(a1),-sin(a1),cos(a1));\n",
    "        mat2 rot2=mat2(cos(a2),sin(a2),-sin(a2),cos(a2));\n",
    "        dir.xz*=rot1;\n",
    "        dir.xy*=rot2;\n",
    "        vec3 from=vec3(1.,.5,0.5);\n",
    "        from+=vec3(time*2.,time,-2.);\n",
    "        from.xz*=rot1;\n",
    "        from.xy*=rot2;\n",
    "\n",
    "        //volumetric rendering\n",
    "        //音量渲染\n",
    "        float s=0.1,fade=1.;\n",
    "        vec3 v=vec3(0.);\n",
    "        for (int r=0; r<volsteps; r++) {\n",
    "            vec3 p=from+s*dir*.5;\n",
    "            p = abs(vec3(tile)-mod(p,vec3(tile*2.))); // 平铺褶皱——tiling fold\n",
    "            float pa,a=pa=0.;\n",
    "            for (int i=0; i<iterations; i++) { \n",
    "                p=abs(p)/dot(p,p)-formuparam; // 神奇的公式——the magic formula\n",
    "                a+=abs(length(p)-pa); //平均变化的绝对和——absolute sum of average change\n",
    "                pa=length(p);\n",
    "            }\n",
    "            float dm=max(0.,darkmatter-a*a*.001); //暗物质——dark matter\n",
    "            a*=a*a; // add contrast\n",
    "            if (r>6) fade*=1.-dm; // 暗物质，不要靠近——dark matter, don't render near\n",
    "            //v+=vec3(dm,dm*.5,0.);\n",
    "            v+=fade;\n",
    "            v+=vec3(s,s*s,s*s*s*s)*a*brightness*fade; // 基于距离的着色——coloring based on distance\n",
    "            fade*=distfading; // 距离衰减——distance fading\n",
    "            s+=stepsize;\n",
    "        }\n",
    "        v=mix(vec3(length(v)),v,saturation); //颜色调整——color adjust\n",
    "        fragColor = vec4(v*.01,1.);\t\n",
    "\n",
    "    }\n",
    "    \n",
    "\"\"\", 533, 300)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "l0 = Label('Star Nest by Pablo Roman Andrioli', x=10, y=10, color='cyan')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@app.event\n",
    "def render(ct, dt):\n",
    "    \n",
    "    st.render(ct, dt)\n",
    "    l0.render()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sample = Sample('../docs/_static/audio/tb303.5.ogg', loop=True, amp=8.)\n",
    "sample.play()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "app.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "nbTranslate": {
   "displayLangs": [
    "en"
   ],
   "hotkey": "alt-t",
   "langInMainMenu": true,
   "sourceLang": "en",
   "targetLang": "cn",
   "useGoogleTranslate": true
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
